import { defineStore } from 'pinia'
import { usePlayerStore } from './player'

export const usePlaylistStore = defineStore('playlist', {
  state: () => ({
    playlist: [], // 播放列表
    history: [], // 播放历史
  }),
  
  getters: {
    // 获取播放列表中的歌曲数量
    playlistCount: (state) => state.playlist.length,
    
    // 检查歌曲是否在播放列表中
    isSongInPlaylist: (state) => (songId) => {
      return state.playlist.some(song => song.id === songId)
    },
    
    // 获取当前播放歌曲在播放列表中的索引
    currentSongIndex: (state) => {
      const playerStore = usePlayerStore()
      const currentSongId = playerStore.currentSong.id
      
      if (!currentSongId) return -1
      
      return state.playlist.findIndex(song => song.id === currentSongId)
    }
  },
  
  actions: {
    // 添加歌曲到播放列表
    addToPlaylist(song) {
      // 如果歌曲已经在播放列表中，则不重复添加
      if (this.isSongInPlaylist(song.id)) return false
      
      this.playlist.push(song)
      return true
    },
    
    // 从播放列表中移除歌曲
    removeFromPlaylist(songId) {
      const playerStore = usePlayerStore()
      const index = this.playlist.findIndex(song => song.id === songId)
      
      if (index === -1) return false
      
      // 从播放列表中移除
      this.playlist.splice(index, 1)
      
      // 如果删除的是当前播放的歌曲，且播放列表不为空，则播放下一首
      if (playerStore.currentSong.id === songId && this.playlist.length > 0) {
        // 计算下一首要播放的歌曲索引
        const nextIndex = index >= this.playlist.length ? 0 : index
        const nextSong = this.playlist[nextIndex]
        
        // 播放下一首
        playerStore.playSong({
          songId: nextSong.id,
          songName: nextSong.title,
          artist: nextSong.artist,
          coverUrl: nextSong.coverUrl,
          musicUrl: nextSong.musicUrl,
          album: nextSong.album,
          isLiked: nextSong.isFavorite === 1
        })
      } else if (playerStore.currentSong.id === songId && this.playlist.length === 0) {
        // 如果播放列表为空且删除的是当前播放的歌曲，则停止播放
        playerStore.stopPlay()
      }
      
      return true
    },
    
    // 清空播放列表
    clearPlaylist() {
      const playerStore = usePlayerStore()
      
      this.playlist = []
      
      // 停止当前播放
      if (playerStore.isPlaying) {
        playerStore.stopPlay()
      }
    },
    
    // 播放播放列表中的歌曲
    playFromPlaylist(song) {
      const playerStore = usePlayerStore()
      
      // 播放歌曲
      playerStore.playSong({
        songId: song.id,
        songName: song.title,
        artist: song.artist,
        coverUrl: song.coverUrl,
        musicUrl: song.musicUrl,
        album: song.album,
        isLiked: song.isFavorite === 1
      })
      
      // 添加到播放历史
      this.addToHistory(song)
    },
    
    // 添加到播放历史
    addToHistory(song) {
      // 如果已在历史记录中，先移除旧记录
      const historyIndex = this.history.findIndex(item => item.id === song.id)
      if (historyIndex !== -1) {
        this.history.splice(historyIndex, 1)
      }
      
      // 添加到历史记录开头
      this.history.unshift(song)
      
      // 仅保留最近的 50 条记录
      if (this.history.length > 50) {
        this.history = this.history.slice(0, 50)
      }
    },
    
    // 播放列表排序
    reorderPlaylist(oldIndex, newIndex) {
      if (oldIndex < 0 || oldIndex >= this.playlist.length || 
          newIndex < 0 || newIndex >= this.playlist.length) {
        return false
      }
      
      // 获取要移动的元素
      const item = this.playlist[oldIndex]
      
      // 移除原位置的元素
      this.playlist.splice(oldIndex, 1)
      
      // 添加到新位置
      this.playlist.splice(newIndex, 0, item)
      
      return true
    }
  },
  
  persist: {
    key: 'music-playlist',
    storage: localStorage,
    paths: ['playlist', 'history'] // 指定需要持久化的状态
  }
}) 